package com.yhx.tank;

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.*;



public class TankFrame extends Frame {
	Tank myTank=new Tank(200,400,Dir.DOWN,this);
	List<Bullet> bullets=new ArrayList<>();
	List<Tank> tanks=new ArrayList<>();
	
	//Bullet b=new Bullet(300,300,Dir.DOWN);
	static final int GAME_WIDTH=800, GAME_HEIGHT=600;
	public TankFrame() {
		setSize(GAME_WIDTH,GAME_HEIGHT);
		setTitle("tank war");
		setResizable(false);
		setVisible(true);
		
		
		addWindowListener(new WindowAdapter(){ //�Ҽ�Source--Override--windowClosing
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				System.exit(0);
			}
		});
		
		
		addKeyListener(new KeyAdapter() {

			boolean BL=false,BU=false,BR=false,BD=false;
			@Override
			public void keyPressed(KeyEvent e) {
				int key=e.getKeyCode();
				switch(key) {
				case KeyEvent.VK_LEFT:
					BL=true;
					break;
					
				case KeyEvent.VK_RIGHT:
					BR=true;
					break;
				
				case KeyEvent.VK_UP:
					BU=true;
					break;
				
				case KeyEvent.VK_DOWN:
					BD=true;
					break;
					
					default:
						break;
				}
//				repaint();
				setMainTankDir();
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
				int key=e.getKeyCode();
				switch(key) {
				case KeyEvent.VK_LEFT:
					BL=false;
					break;
					
				case KeyEvent.VK_RIGHT:
					BR=false;
					break;
				
				case KeyEvent.VK_UP:
					BU=false;
					break;
				
				case KeyEvent.VK_DOWN:
					BD=false;
					break;
					
					
				case KeyEvent.VK_CONTROL:
					myTank.fire();//发射子弹
					break;	
					
				default:
					break;
				}
				setMainTankDir();
			}
			
			
			private void setMainTankDir() {
				if(!BL&&!BU&&!BR&&!BD)
					myTank.setMoving(false);
				else {
				myTank.setMoving(true);
				if(BL)   myTank.setDir(Dir.LEFT);
				if(BU)   myTank.setDir(Dir.UP);
				if(BR)   myTank.setDir(Dir.RIGHT);
				if(BD)   myTank.setDir(Dir.DOWN);
				}
				
				
			}
			
		});
	}
	
	
	
	Image offScreenImage=null;
	@Override
	public void update(Graphics g) {
		if(offScreenImage==null) {
			offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);
		}
		Graphics gOffScreen=offScreenImage.getGraphics();
		Color c=gOffScreen.getColor();
		gOffScreen.setColor(Color.BLACK);
		gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
		gOffScreen.setColor(c);
		paint(gOffScreen);
		g.drawImage(offScreenImage,0,0,null);
		
		
	}
	
	@Override
	public void paint(Graphics g) {
		
		Color c=g.getColor();
		g.setColor(Color.WHITE);
		g.drawString("子弹数量:"+bullets.size(), 10, 60);
		g.drawString("敌方数量:"+tanks.size(), 10, 80);
		g.setColor(c);
		
		myTank.paint(g);
//		b.paint(g);
		for(int i=0;i<bullets.size();i++) {
			bullets.get(i).paint(g);
		}
		
		
		
		//画敌方的坦克
		for(int i=0;i<tanks.size();i++) {
			tanks.get(i).paint(g);
		}
		
		
		for(int i=0;i<bullets.size();i++) {
			for(int j=0;j<tanks.size();j++) {
				bullets.get(i).collideWith(tanks.get(j));
			}
		}
		
	}
	
	

}
